﻿using Medicine.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Medicine.Entity;
using Medicine.Service;
using Medicine.View;
namespace Medicine
{
    public partial class UserForm : Form
    {
        private User user;
        private UpdateUser uu;
        private Record rec;
        private AddmedInfo ami;
        public static UserForm uf;
        public static List<Dictionary<String, Object>> winFormList;

        public UserForm(User user)
        {
            //设置用户
            this.user = user;
            //将本对象加入list，方便管理
           // Dictionary<String, Object> duf = new Dictionary<string, object>();
          //  duf.Add("UserForm",this); 
            InitializeComponent();
         
            uf = this; 
            //设置用户名
            label2.Text = user.Name;
            //对listview进行初始化
            listView1.Items.Clear();
            showMedicine();

        }
        public void reflush() {
            label2.Text = user.Name;
            showMedicine();
        }
        public void showMedicine()
        {
            /// DbHelper.insert("insert into medicine (name,info,price,ison) values('66','5416465',2,6)");
            //MedicineDao medi = new MedicineDao();
            // List<Dictionary<String, Object>> med_list =  medi.query("select * from medicine");
            //Dictionary<String, Object> dic = med_list[0];
            /*String name = dic["name"].ToString();
            MessageBox.Show(name);
           */
            MedicineServie ms = new MedicineServie();
            List<Medicin> med_list = ms.queryMedAll();
            //MessageBox.Show(med_list.Count.ToString());
            foreach (Medicin med in med_list)//遍历查询出来的数据
            {
                ListViewItem lv = new ListViewItem();
                lv.Text = med.Id.ToString();//设置第一行显示的数据
                //绑定剩余列的数据
                lv.SubItems.Add(med.Name);
                lv.SubItems.Add(med.Price.ToString());
                lv.SubItems.Add(med.Information);
               
                //一定记得行数据创建完毕后添加到列表中
                listView1.Items.Add(lv);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (null == this.uu || uu.IsDisposed)
            {
                this.uu = new UpdateUser(this.user);
               // Dictionary<String, Object> duu = new Dictionary<string, object>();
               // duu.Add("UpdateUser", this);
                uu.Show();
            }
            else uu.Activate();
            //this.Close();
            
        }
        /// <summary>
        /// 查看记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (null == rec || rec.IsDisposed)
            {
                rec = new Record(this.user);
                rec.Show();
            }
            else rec.Activate();
            //this.Close();
        
        }
        /// <summary>
        /// 切换用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1();
            this.Close();
            f.Show();
        }


   
        /// <summary>
        /// 购买药品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lv in listView1.SelectedItems)
            {
                // MessageBox.Show(lv.Text + "\t" + lv.SubItems[3].Text);
                Medicin medicine = new Medicin();
                medicine.Id = int.Parse(lv.Text.ToString());
                medicine.Name = lv.SubItems[1].Text;
                medicine.Price = float.Parse(lv.SubItems[2].Text.ToString());
                if (null == ami || ami.IsDisposed)
                {
                    ami = new AddmedInfo(medicine, user);
                    ami.Show();
                }
                else ami.Activate();
               
            }

        }

        private void UserForm_Load(object sender, EventArgs e)
        {
            label4.Text = System.DateTime.Now.ToString();
            //timer1.Interval = 1;
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            label4.Text = System.DateTime.Now.ToString();
        }
    }
}
